package com.lanxin.jet.tools;

public class JetCriteria {
	
	//条件连接方式
    public final static String OPT_AND      = "AND";
    public final static String OPT_OR       = "OR";

    //排序方式
    public final static String ORDER_ASC    = "ASC";
    public final static String ORDER_DESC   = "DESC";

    //联合方式
    public final static String JOIN_INNER   = "inner";
    public final static String JOIN_LEFT    = "left";
    public final static String JOIN_RIGHT   = "right";

    //字段
    private String select = "*";
	//联合
	private String union = "";
    //条件
	private String condition = "";
	//排序
	private String order = "";
	//分组
	private String group = "";
	//分页
    private String page = "";
    
    /**
     * 设置查询字段
     * @param select
     */
    public void setSelect(String select) {
        if (select != null && !select.isEmpty()) {
            this.select = select;
        }
    }

    public String getSelect() {
        return select;
    }

    /**
     * 添加查询条件
     * @param field 字段
     * @param value 值
     */
    public void add(String field, String value) {
        if (value != null && !value.isEmpty()) {
            addCondition(field + "='" + addSlashes(value) +"'", OPT_AND);
        }
    }

    /**
     * 添加查询条件
     * @param field 字段
     * @param value 值
     */
    public void add(String field, int value) {
        addCondition(field + "=" + value, OPT_AND);
    }

    /**
     * 添加查询条件
     * @param field     字段
     * @param value     值
     * @param symbol    连接符号
     */
    public void add(String field, String value, String symbol) {
        if (value != null && !value.isEmpty()) {
            addCondition(field + " " + symbol + " '" + addSlashes(value) +"'", OPT_AND);
        }
    }

    /**
     * 添加查询条件
     * @param field     字段
     * @param symbol    连接符号
     * @param value     值
     */
    public void add(String field, String symbol, int value) {
        addCondition(field + " " + symbol + " " + value, OPT_AND);
    }

    /**
     * 添加查询条件(OR)
     * @param field 字段
     * @param value 值
     */
    public void addOR(String field, String value) {
        if (value != null && !value.isEmpty()) {
            addCondition(field + "='" + addSlashes(value) +"'", OPT_OR);
        }
    }

    /**
     * 添加查询条件(OR)
     * @param field 字段
     * @param value 值
     */
    public void addOR(String field, int value) {
        addCondition(field + "=" + value, OPT_OR);
    }

    /**
     * 添加查询条件(OR)
     * @param field     字段
     * @param value     值
     * @param symbol    连接符号
     */
    public void addOR(String field, String symbol, String value) {
        if (value != null && !value.isEmpty()) {
            addCondition(field + " " + symbol + " " + value, OPT_OR);
        }
    }

    /**
     * select IN 查询条件
     * @param field
     * @param args
     */
    public void addIn(String field, String args[]) {
        if (args.length > 0) {
            addCondition(field + " IN ('" + String.join("','", args) + "')", OPT_AND);
        }
    }

    /**
     * select IN查询
     * @param field
     * @param args
     */
    public void addIn(String field, int args[]) {
        if (args.length > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int item : args) {
                stringBuilder.append("," + item);
            }
            addCondition(field + " IN (" + stringBuilder.toString().substring(1) + ")", OPT_AND);
        }
    }

    /**
     * Like模糊查询
     * @param field
     * @param value
     */
    public void addLike(String field, String value) {
        if (value != null && !value.isEmpty()) {
            addCondition(field + " like '%" + addSlashes(value) +"%'", OPT_AND);
        }
    }

    /**
     * 添加自定义查询条件
     * @param condition 条件
     * @param opt       操作符
     */
    public void addCondition(String condition, String opt) {
        if (! this.condition.isEmpty()) {
			this.condition = "(" + this.condition + ") " + opt + " (" + condition + ")";
		} else {
			this.condition = condition;
		}
    }

    /**
	 * 过滤SQL注入字符
	 * @param str
	 * @return
	 */
	private String addSlashes(String str) {
        str = str.replace("\\", "\\\\");
		str = str.replace("\'", "\\'");
		str = str.replace("\"", "\\\"");
		return str;
	}

    /**
     * 联合查询
     * @param tableName 联合表名称
     * @param foreign   外键关系
     * @param type      联合方式 - inner | left | right
     */
    public void setUnion(String tableName, String foreign, String type) {
        if (this.union.isEmpty()) {
            this.union = type + " join " + tableName + " on " + foreign;
        } else {
            this.union += " " + type + " join " + tableName + " on " + foreign;
        }
    }

    /**
     * 按字段排序
     * @param field
     * @param type
     */
    public void orderBy(String field, String type) {
        this.order = field + " " + type;
    }

    /**
     * 按字段分组
     * @param field
     */
    public void groupBy(String field) {
        this.group = field;
    }

    /**
     * 设置分页
     * @param offset
     * @param limit
     */
    public void setPage(int offset, int limit) {
        if (limit == 0) {
            limit = 1;
        }
        this.page = offset + "," + limit;
    }
    
    /**
     * 构建表查询语句
     * @return
     */
    public String getCondition() {
		if (! condition.isEmpty()) {
			return " where " + condition;
		} else {
			return "";
		}
    }

    /**
     * 构建查询条件
     * @return
     */
    public String getFullCondition() {
        StringBuilder stringBuilder = new StringBuilder();
        if (!this.union.isEmpty()) {
            stringBuilder.append(this.union);
        }
        if (!this.condition.isEmpty()) {
            stringBuilder.append(" where " + this.condition);
        }
        if (!this.group.isEmpty()) {
            stringBuilder.append(" group by " + this.group);
        }
        if (!this.order.isEmpty()) {
            stringBuilder.append(" order by " + this.order);
        }
        if (!this.page.isEmpty()) {
            stringBuilder.append(" limit " + this.page);
        }
        return stringBuilder.toString();
    }
	
}